<?php
/**
 * Created by PhpStorm.
 * User: 牛赞赞
 * Date: 2020/12/31
 * Time: 10:31
 */

namespace App\Game\Logic;


use App\Game\Conf\GameProgress;
use App\Game\Conf\MainCmd;
use App\Game\Conf\SubCmd;
use App\Game\Core\AStrategy;
use App\Game\Core\Djs;
use App\Game\Core\Message;
use App\Game\Core\Packet;
use App\Game\Core\Robot;
use App\Game\Core\Room;
use App\Game\Timer\GameTimer;
use App\Model\User;
use App\Service\Lock;
use App\Task\GameSyncTask;

class GamePrepare extends AStrategy
{
    /**
     * 游戏等待准备
     */
    public function exec()
    {
        $account = $this->_params['userinfo']['account'];
        $fd = $this->_params['userinfo']['fd'];
        $room_no = $this->_params['data']['room_no'];
        $serv = server();
        if (!$room_no) {
            $msg = ['status' => 1, 'info' => '房间号不存在'];
            pushErr($serv, $fd, SubCmd::SUB_GAME_PREPARE_RESP, $msg);
            return;
        }
        $progress = getGameProgress($room_no);
        if (!in_array($progress, [GameProgress::GAME_JOIN, GameProgress::GAME_PREPARE, GameProgress::GAME_CACULAR])) {
            $msg = array('status' => 1, 'info' => '准备非法, 还未到准备阶段！');
            pushErr($serv, $fd, SubCmd::SUB_GAME_PREPARE_RESP, $msg);
            return;
        }
        $arr = getGamePrepareALL($room_no);
        if (count($arr) > 4) {
            $msg = ['status' => 2, 'info' => '房间已满'];
            pushErr($serv, $fd, SubCmd::SUB_GAME_PREPARE_RESP, $msg);
            return;
        }
        if (!$account) {
            $msg = ['status' => 3, 'info' => '获取用户信息失败'];
            pushErr($serv, $fd, SubCmd::SUB_GAME_PREPARE_RESP, $msg);
            return;
        }

        //玩家准备
        playerGamePrepare($room_no, $account);

        $chair_id = getChairIdbyAccount($room_no, $account); // 座位号
        if (!$chair_id) {
            setGameProgressType($room_no, 1);//设为单机
            $chair_id = Room::addRoomChair($account, $room_no);
        }
        $arr = getGamePrepareALL($room_no);
//        $master = Room::getRoomMaster($room_no);
//        dd('master', $master);
        ddNode('p1');
        if (count($arr) < 4) {
            ddNode('p2');
            $msg = ['status' => 0, 'chair_id' => $chair_id, 'master' => 0];
            Message::broadCast($room_no, $msg, SubCmd::SUB_GAME_PREPARE_RESP);
        }

        setGameProgress($room_no, GameProgress::GAME_PREPARE);

        $robots = Robot::getRoomRobot($room_no);
        $robotNum = count($robots);
        $game_type = Room::getRoomData($room_no, 'game_type');
        if (empty($game_type)) {
            $game_type = 0;
        }
        if ($game_type == 1) { //大奖赛
            $waiters = Room::getRoomWait($room_no);
            $chairs = Room::getRoomChairs($room_no);

            foreach ($waiters as $acc) {
                if (Robot::isRobot($room_no, $acc)) {
//                    dd('robot prepare ', $acc);
//                    dd('robot chair_id ', $chairs[$acc]);
                    $prepare = getGamePrepare($room_no, $acc);
                    if (!$prepare) {
                        playerGamePrepare($room_no, $acc);
                        $chair_id = $chairs[$acc];
                        //发送准备消息
                        $msg = ['status' => 0, 'chair_id' => $chair_id, 'master' => 0];
                        Message::broadcast($room_no, $msg, SubCmd::SUB_GAME_PREPARE_RESP);
                        sleep(rand(1, 2));
                    }
                    continue;
                }
                $playerInfo = Djs::getDjsPlayerData($acc);
                $playerInfo['progress'] = Djs::$progress_game_prepare;
                Djs::updatePlayerData($acc, $playerInfo);
            }
        }

        $ptype = getGameProgressType($room_no);
        if ($ptype && ($game_type == 0)) {
            ddNode('p3');
//            GameTimer::do_assign_robots($room_no);
            $wait_time = 2 * 1000;
            \Swoole\Timer::after((int)$wait_time, function () use ($room_no) {
                Robot::addSingleRobot($room_no);
            });
            return;
        }
        $realNum = 4 - $robotNum;
        $arr = getGamePrepareALL($room_no);
        if (count($arr) >= $realNum) {
//            ddNode(3);
//            dd('arr', $arr);
            $preparArr = [];
            foreach ($arr as $acc => $v) {
                if (Robot::isRobot($room_no, $acc)) {
                    continue;
                }
                $preparArr[$acc] = $v;
            }
//            dd('preparArr', $preparArr);
            $index = 0;
            foreach ($preparArr as $acc => $v) {
                $index++;
                if ($index == $realNum) {
                    if ($acc == $account) {
                        setGameProgress($room_no, GameProgress::GAME_CHANGE_CARD);
                        $master = rand(1, 4);
                        $msg = ['status' => 0, 'chair_id' => $chair_id, 'master' => $master];
                        Message::broadCast($room_no, $msg, SubCmd::SUB_GAME_PREPARE_RESP);
                        //发牌
                        $task = container()->get(GameSyncTask::class);
                        $task->assignCards($room_no, $master);
                    }

                }
            }
        }

    }


}